package net.chrisjrock.ui
{
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.filters.ColorMatrixFilter;
	import flash.geom.ColorTransform;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;

	public class AudioTrackUiComponent
	{
		public static const BUTTON_SIZE:Number = 15;
		public static const SLIDER_MAX_X:Number = 300;
		
		
		private var _text:String;
		private var _fillColor:uint;
		private var _slider:Boolean;
		
		private var _display:Sprite;
		private var _button:Sprite;
		
		private var _enabled:Boolean = true;
		private var _highlighted:Boolean;
		
		public function AudioTrackUiComponent(text:String , fillColor:uint , slider:Boolean)
		{
			_text = text;
			_fillColor = fillColor;
			_slider = slider;
		}
		
		public function destroy ():void
		{
			if (_display && _display.parent)
			{
				_display.parent.removeChild(_display);
			}
			_display = null;
			
			_button = null;
		}
		
		public function init ():void
		{
			_display = new Sprite();
			
			_button = new Sprite();
			_display.addChild(_button);
			
			var label:TextField = getLabel(_text);
			label.textColor = _fillColor;
			label.x = -label.width;
			_button.addChild(label);
			
			_button.graphics.lineStyle(1);
			_button.graphics.beginFill(0 , 1);
			_button.graphics.drawRect(label.x , 0 , label.width , label.height);
			_button.graphics.beginFill(_fillColor);
			_button.graphics.drawRect(0 , 0 , BUTTON_SIZE , BUTTON_SIZE);
			
			_button.buttonMode = true;
			_button.mouseChildren = false;
			
			refreshButtonHighlighted();
			refreshButtonEnabled();
			
			if (!_slider)
			{
				return;
			}
			
			var slidePath:Shape = getSlidePath();
			_display.addChildAt(slidePath , 0);
			
			slidePath.y = BUTTON_SIZE * .5;
		}
		
		public function setSlidePos (pos:Number):void
		{
			_button.x = SLIDER_MAX_X * pos;
		}
		
		/**
		 * @private
		 * Generates a text label. 
		 * @param text
		 * @return 
		 */
		private function getLabel (text:String):TextField
		{
			var label:TextField = new TextField();
			label.autoSize = TextFieldAutoSize.LEFT;
			label.textColor = 0xffffff;
			label.selectable = false;
			var textFormat:TextFormat = new TextFormat();
			textFormat.bold = "bold";
			label.defaultTextFormat = textFormat;
			label.text = text;
			return label;
		}
		
		/**
		 * @private
		 * Generates a straight line display to be used as the path for volume or scrub controls.
		 * @return 
		 */
		private function getSlidePath ():Shape
		{
			var slidePath:Shape = new Shape();
			slidePath.graphics.lineStyle(1 , 0x404040);
			slidePath.graphics.lineTo(SLIDER_MAX_X , 0);
			return slidePath;
		}
		
		/**
		 * @private
		 * Sets the highlight state of a button (visual only).
		 * @param button
		 * @param enabled
		 */
		private function refreshButtonHighlighted ():void
		{
			if (_highlighted)
			{
				_button.transform.colorTransform = new ColorTransform();
			}
			else
			{
				_button.transform.colorTransform = new ColorTransform(.5 , .5 , .5);
			}
		}
		
		/**
		 * @private
		 * Sets the enabled state of a button (controls interactivity).
		 * @param button
		 * @param enabled
		 */
		private function refreshButtonEnabled ():void
		{
			_button.mouseEnabled = enabled;
			
			if (enabled)
			{
				_button.filters = null;
			}
			else
			{
				//Desaturates
				var r:Number = 1 / 3;
				var colorMatrix:Array = 
					[
						r , r , r , 0 , 0 ,
						r , r , r , 0 , 0 ,
						r , r , r , 0 , 0 ,
						0 , 0 , 0 , 1 , 0
					];
				
				_button.filters = [new ColorMatrixFilter(colorMatrix)];
			}
		}

		public function get type():String
		{
			return _text;
		}
		
		public function get fillColor():uint
		{
			return _fillColor;
		}
		
		public function get slider():Boolean
		{
			return _slider;
		}

		public function get display():Sprite
		{
			return _display;
		}
		
		public function get button ():Sprite
		{
			return _button;
		}

		public function get enabled():Boolean
		{
			return _enabled;
		}

		public function set enabled(value:Boolean):void
		{
			if (_enabled == value)
			{
				return;
			}
			
			_enabled = value;
			
			refreshButtonEnabled();
		}

		public function get highlighted():Boolean
		{
			return _highlighted;
		}

		public function set highlighted(value:Boolean):void
		{
			if (_highlighted == value)
			{
				return;
			}
			
			_highlighted = value;
			
			refreshButtonHighlighted();
		}

	}
}